home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 718 < prev    next >
Internet Message Format  |  1996-08-06  |  5KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.std.c
  4. Subject: Re: Restrictions on qsort compare function?
  5. Date: Tue, 09 Apr 96 16:51:22 GMT
  6. Organization: none
  7. Message-ID: <829068682snz@genesis.demon.co.uk>
  8. References: <4kbl1l$74r@sam.inforamp.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4kbl1l$74r@sam.inforamp.net>
  15.            pcurran@inforamp.net "Peter Curran" writes:
  16.  
  17. >On Mon, 08 Apr 96 13:56:53 GMT in article <828971813snz@genesis.demon.co.uk>
  18. >    Lawrence Kirby <fred@genesis.demon.co.uk> (Lawrence Kirby) wrote:
  19. >
  20. >>>IMHO, qsort() is required to return an array that is sorted according to the
  21. >>>criteria implied by the comparison function.  That is, at the point where
  22. > qsort
  23. >>>returns, the array must match the order implied by the comparison function.
  24. >
  25. >The comparison function I postulated *is* consistent, at any instant in time.
  26.  
  27. Unless it is consistent over the entire duration of the sort (it defines
  28. the sorted ordering of the elements subject to equal keys) it doesn't
  29. form a valid relation for a sort.
  30.  
  31. >(Just to be clear - another correspondent suggested my original posting may not
  32. >have said quite what I intended.  The comparison function I suggested., or
  33. >intended, calculates as result by comparison of the values pointed at by the
  34. >parameters, in a way we can all agree is acceptable.  However, it then calls
  35. >time(), and if the result it odd it negates the result before returning it.  In
  36. >a conventional implementation of time(), it reverses the order every second,
  37. > but
  38. >defines a consistent order at any given time.)
  39.  
  40. Or use rand()! However the relation is defined purely on the values of the
  41. keys. The standard embodies this idea too -
  42.  
  43. "The function shall return an integer less than, equal to, or greater than
  44.  zero if the first argument is considered to be respectively less than, equal
  45.  to, or greater than the second"
  46.  
  47. gives no license for the comparison function to consider anything other than
  48. its arguments in determining the return value. I would certainly accept that
  49. this is worded badly i.e. "first argument" should read "object pointed to by
  50. the first argument" and similarly for "second". However this loose usage
  51. appears elsewhere in the standard and I believe the intent is clear (I'm
  52. happy to discuss that further).
  53.  
  54. >>The critical word is 'sorted'. I suggest you read section 5 (i.e. the
  55. >>first section of chapter 5) in Knuth Vol 3 to get a reasonable idea of what
  56. >>a sort is. 
  57. >
  58. >I can assure you that I read Knuth cover to cover, within a few weeks of its
  59. >first publication.  I really don't think this kind of insult is necessary here.
  60.  
  61. No insult was intended - apologies, I could have written that better.
  62. My point stands though that sorting is a well defined concept and there
  63. is every reason to assume that a formal standards document will use terms in
  64. their correct or formal sense. Otherwise they don't have any well-defined
  65. meaning at all.
  66.  
  67. >There isn't much point in continuing this.  We will have to agree to disagree.
  68. >I understand how you are reaching the conclusions you are reaching (and I agree
  69. >it should be possible to reach them).  However I think that you are stretching
  70. >the text of the standard beyond the breaking point to get there.
  71.  
  72. My argument is based fundamentally on 3.16:
  73.  
  74. "Undefined behaviour is otherwise indicated in this International Standard
  75.  by the words ``undefined behaviour'' or by the omission of any explicit
  76.  defintion of behaviour. There is no difference in emphasis among these
  77.  three; they all describe ``behaviour that is undefined.''"
  78.  
  79. This means that code has undefined behaviour unless you can show what the
  80. defined behaviour is according to the standard. In all of your examples
  81. the behaviour can vary depending on the actual implementation of qsort().
  82. In the absense of any other limitation in the standard (such as a statement
  83. of implementation-defined behaviour), this means the behaviour is undefined.
  84.  
  85. To disprove my assertion for any of your examples the bottom line is to
  86. show, by reference to the standard, what the defined behaviour is. An
  87. example is putting forward a reasonable definition of 'sort' which would
  88. make the example well defined.
  89.  
  90. Something else to consider is that the bsearch() function defines a
  91. comparison function in essentially the same way as the qsort() function
  92. does (except that it refers correctly to the key object and array element
  93. rather than just 'arguments'), so a valid qsort() comparison function
  94. ought to be a valig bsearch() comparison function.
  95.  
  96. -- 
  97. -----------------------------------------
  98. Lawrence Kirby | fred@genesis.demon.co.uk
  99. Wilts, England | 70734.126@compuserve.com
  100. -----------------------------------------
  101.